from pages.base_page import BasePage
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select


class AddBusinessPage(BasePage):
    url = "/crm/index.php?m=business&a=add"
    result_locator = (By.XPATH, '/html/body/div[5]/div[2]')
    error_locator =(By.XPATH,"/html/body/div/form/fieldset/div[1]")
    owner_name_locator = (By.ID, 'owner_name')  # 负责人
    customer_name_locator = (By.ID, 'customer_name')  # 客户
    fields_locator = (By.ID, 'fields')  # 筛选条件
    searchs_locator = (By.ID, 'searchs')  # 搜索关键字
    search_button_locator = (By.XPATH, '//*[@id="dialog-message"]/div/ul/li[4]/button')  # 搜索键
    choose_locator = (By.XPATH, '//*[@id="datas"]/tr/td[1]/input[1]')  # 选择客户
    queren_locator = (By.XPATH, '/html/body/div[10]/div[3]/div/button[1]')  # 确认按钮
    total_price_locator = (By.ID, 'total_price')  # 商机金额
    name_locator = (By.NAME, 'name')  # 商机名
    contacts_name_locator = (By.ID, 'contacts_name')  # 联系人
    type_locator = (By.ID, 'type')  # 商机类型
    status_locator = (By.ID, 'status_id')  # 状态
    origin_locator = (By.ID, 'origin')  # 商机来源
    gain_rate_locator = (By.ID, 'gain_rate')  # 赢单率
    estimate_price_locator = (By.ID, 'estimate_price')  # 预计成交价
    nextstep_time_locator = (By.ID, 'nextstep_time')  # 下次联系时间
    nextstep_locator = (By.ID, 'nextstep')  # 下次联系内容
    description_locator = (By.XPATH, '//*[@id="description"]')  # 备注
    save_locator = (By.XPATH, '//*[@id="form1"]/table/tfoot/tr/td/input[1]')  # 保存

    def input_owner_name(self,owner_name):
        """
        输入负责人
        """
        owner_name_element = self.find_element(self.owner_name_locator)
        owner_name_element.clear()
        owner_name_element.send_keys(owner_name)

    def click_customer_name(self):
        """
        点击客户选框
        """
        self.find_element(self.customer_name_locator).click()

    def select_fields(self,fields_name):
        """
        选择筛选条件
        """
        fields_element = Select(self.find_element(self.fields_locator))
        fields_element.select_by_value(fields_name)


    def input_search(self,customer_key):
        """
        输入搜索关键词
        """
        self.find_element(self.searchs_locator).send_keys(customer_key)

    def click_search_btn(self):
        """
        点击搜索键
        """
        self.find_element(self.search_button_locator).click()

    def click_choose(self):
        """
        选择第一个客户
        """
        self.find_element(self.choose_locator).click()

    def click_queren_btn(self):
        """
        点击确认
        """
        self.find_element(self.queren_locator).click()

    def input_business_price(self,business_price):
        """
        输入商机金额
        """
        self.find_element(self.total_price_locator).send_keys(business_price)

    def input_business_name(self,business_name):
        """
        输入商机名
        """
        self.find_element(self.name_locator).send_keys(business_name)

    def clear_contacts_name(self):
        """
        清除联系人
        """
        self.find_element(self.contacts_name_locator).clear()

    def input_contacts_name(self,contacts_name):
        """
        输入联系人
        """
        self.find_element(self.contacts_name_locator).send_keys(contacts_name)

    def select_type(self,type_value):
        """
        选择商机类型
        """
        type_element = Select(self.find_element(self.type_locator))
        type_element.select_by_value(type_value)

    def input_status(self,status):
        """
        输入状态
        """
        self.find_element(self.status_locator).send_keys(status)


    def input_origin(self,origin):
        """
        输入商机来源
        """
        self.find_element(self.origin_locator).send_keys(origin)

    def input_gain_rate(self,gain_rate):
        """
        输入赢单率
        """
        self.find_element(self.gain_rate_locator).send_keys(gain_rate)

    def input_estimate_price(self,estimate_price):
        """
        输入预计成交价
        """
        self.find_element(self.estimate_price_locator).send_keys(estimate_price)

    def input_nextstep_time(self,nextstep_time):
        """
        输入下次联系时间
        """
        self.find_element(self.nextstep_time_locator).send_keys(nextstep_time)

    def input_nextstep(self,nextstep):
        """
        输入下次联系内容
        """
        self.find_element(self.nextstep_locator).send_keys(nextstep)

    def input_description(self,description):
        """
        输入备注
        """
        self.find_element(self.description_locator).send_keys(description)

    def click_save(self):
        """
        点击保存
        """
        self.find_element(self.save_locator).click()


#返回增删改查后的提示信息
    def get_result_tips(self):
        return self.find_element(self.result_locator).text

#返回弹窗文本信息
    def get_alert_text(self):
        return self.driver.switch_to.alert.text

#返回报错弹窗文本信息
    def get_error_text(self):
        return self.find_element(self.error_locator).text

